﻿using DelonixRegia.Classes;
using DelonixRegia.Classes.Reporting;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows;

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.Text;


namespace DelonixRegia
{
    
    public partial class HousekeepingReport : System.Web.UI.Page
    {
        static ArrayList mondaylist = new ArrayList();
        static ArrayList tuesdaylist = new ArrayList();
        static ArrayList wednesdaylist = new ArrayList();
        static ArrayList thursdaylist = new ArrayList();
        static ArrayList fridaylist = new ArrayList();
        static ArrayList saturdaylist = new ArrayList();
        static ArrayList sundaylist = new ArrayList();
        static ArrayList shiftlist = new ArrayList();
        static ArrayList daylist = new ArrayList();
        static ArrayList datelist = new ArrayList();
        protected void Page_Load(object sender, EventArgs e)
        {
            PDFBut.Visible = true;
            ExcelBut.Visible = true;
            PrintBut.Visible = true;
            // CHEW SENG'S CODE - DON'T TOUCH //
            Account a = (Account)Session["acc"];

            if (a != null)
            {
                if (a.userType < 2)
                    Response.Redirect("Default.aspx");
            }
            else
                Response.Redirect("SignIn.aspx");
            // CHEW SENG'S CODE - DON'T TOUCH //

            if (!Page.IsPostBack)
            {
                HKCalendar.SelectedDate = DateTime.Today;
            }


        }



        protected void HKCalendar_DayRender(object sender, DayRenderEventArgs e)
        {
            if (e.Day.IsOtherMonth)
            {
                e.Cell.BackColor = ColorTranslator.FromHtml("#a9a9a9");
                e.Cell.Visible = false;
                e.Cell.ForeColor = Color.White;
                e.Day.IsSelectable = false;
            }
        }

        protected void queryBut_Click(object sender, EventArgs e)
        {
            if (HKCalendar.SelectedDates.Count == 1)
            {
                DateTime date = HKCalendar.SelectedDate;
                string date1 = date.ToString("yyyy-MM-dd");
                string dayofdate = date.ToString("dddd");
                ArrayList shiftlist = reportingDBManager.getShiftDay(date1);
                if (dayofdate.Equals("Monday"))
                {
                    gvMonday.DataSource = shiftlist;
                    gvMonday.DataBind();
                    gvMonday.Caption += ", " + date.ToShortDateString();
                }
                else if (dayofdate.Equals("Tuesday"))
                {
                    gvTuesday.DataSource = shiftlist;
                    gvTuesday.DataBind();
                    gvTuesday.Caption += ", " + date.ToShortDateString();
                }
                else if (dayofdate.Equals("Wednesday"))
                {
                    gvWednesday.DataSource = shiftlist;
                    gvWednesday.DataBind();
                    gvWednesday.Caption += ", " + date.ToShortDateString();
                }
                else if (dayofdate.Equals("Thursday"))
                {
                    gvThursday.DataSource = shiftlist;
                    gvThursday.DataBind();
                    gvThursday.Caption += ", " + date.ToShortDateString();
                }
                else if (dayofdate.Equals("Friday"))
                {
                    gvFriday.DataSource = shiftlist;
                    gvFriday.DataBind();
                    gvFriday.Caption += ", " + date.ToShortDateString();
                }
                else if (dayofdate.Equals("Saturday"))
                {
                    gvSaturday.DataSource = shiftlist;
                    gvSaturday.DataBind();
                    gvSaturday.Caption += ", " + date.ToShortDateString();
                }
                else if (dayofdate.Equals("Sunday"))
                {
                    gvSunday.DataSource = shiftlist;
                    gvSunday.DataBind();
                    gvSunday.Caption += ", " + date.ToShortDateString();
                }

                if (shiftlist.Count == 0)
                {
                    ClientScriptMgr.Execute("$('#errorbox2').html('There are no shifts in this particular date.');$('#errorbox2').hide().show();");
                    PDFBut.Visible = false;
                    ExcelBut.Visible = false;
                    PrintBut.Visible = false;
                }

            }
            else
            {
                
                daylist.Add("Monday");
                daylist.Add("Tuesday");
                daylist.Add("Wednesday");
                daylist.Add("Thursday");
                daylist.Add("Friday");
                daylist.Add("Saturday");
                daylist.Add("Sunday");
                foreach (DateTime dt in HKCalendar.SelectedDates)
                {
                    datelist.Add(dt);
                }
                DateTime dt1 = (DateTime)datelist[0]; //start
                DateTime dt2 = (DateTime)datelist[datelist.Count - 1]; //end
                foreach(string s in daylist){
                    if (s.Equals("Monday"))
                    {
                        
                        mondaylist = reportingDBManager.getShiftWeekMonth(dt1, dt2, "Monday");
                        gvMonday.DataSource = mondaylist;
                        gvMonday.DataBind();
                        
                    }
                    else if (s.Equals("Tuesday"))
                    {
                        tuesdaylist = reportingDBManager.getShiftWeekMonth(dt1, dt2, "Tuesday");
                        gvTuesday.DataSource = tuesdaylist;
                        gvTuesday.DataBind();
                    }
                    else if (s.Equals("Wednesday"))
                    {
                        wednesdaylist = reportingDBManager.getShiftWeekMonth(dt1, dt2, "Wednesday");
                        gvWednesday.DataSource = wednesdaylist;
                        gvWednesday.DataBind();
                    }
                    else if (s.Equals("Thursday"))
                    {
                        thursdaylist = reportingDBManager.getShiftWeekMonth(dt1, dt2, "Thursday");
                        gvThursday.DataSource = thursdaylist;
                        gvThursday.DataBind();
                    }
                    else if (s.Equals("Friday"))
                    {
                        fridaylist = reportingDBManager.getShiftWeekMonth(dt1, dt2, "Friday");
                        gvFriday.DataSource = fridaylist;
                        gvFriday.DataBind();
                    }
                    else if (s.Equals("Saturday"))
                    {
                        saturdaylist = reportingDBManager.getShiftWeekMonth(dt1, dt2, "Saturday");
                        gvSaturday.DataSource = saturdaylist;
                        gvSaturday.DataBind();
                    }
                    else if (s.Equals("Sunday"))
                    {
                        sundaylist = reportingDBManager.getShiftWeekMonth(dt1, dt2, "Sunday");
                        gvSunday.DataSource = sundaylist;
                        gvSunday.DataBind();
                    }

                    if (mondaylist.Count == 0 && tuesdaylist.Count == 0 && wednesdaylist.Count == 0 && thursdaylist.Count == 0 && fridaylist.Count == 0 && saturdaylist.Count == 0 && sundaylist.Count == 0)
                    {
                        ClientScriptMgr.Execute("$('#errorbox2').html('There are no shifts in this particular date range.');$('#errorbox2').hide().show();");
                        PDFBut.Visible = false;
                        ExcelBut.Visible = false;
                        PrintBut.Visible = false;
                    }
                }
                
            }

            pnlFirstStep.Visible = false;
            pnlSecondStep.Visible = true;
            Page.ClientScript.RegisterStartupScript(GetType(), "transitionToSecondStep", "transitionToSecondStep();", true);
        }

        protected void gvMonday_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvMonday.PageIndex = e.NewPageIndex;
            gvMonday.DataSource = mondaylist;
            gvMonday.DataBind();
            
            pnlFirstStep.Visible = false;
            pnlSecondStep.Visible = true;
            Page.ClientScript.RegisterStartupScript(GetType(), "transitionToSecondStep", "transitionToSecondStep();", true);
        }

        protected void gvTuesday_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvTuesday.PageIndex = e.NewPageIndex; 
            gvTuesday.DataSource = tuesdaylist;
            gvTuesday.DataBind();

            pnlFirstStep.Visible = false;
            pnlSecondStep.Visible = true;
            Page.ClientScript.RegisterStartupScript(GetType(), "transitionToSecondStep", "transitionToSecondStep();", true);
        }

        protected void gvWednesday_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvWednesday.PageIndex = e.NewPageIndex;
            gvWednesday.DataSource = wednesdaylist;
            gvWednesday.DataBind();

            pnlFirstStep.Visible = false;
            pnlSecondStep.Visible = true;
            Page.ClientScript.RegisterStartupScript(GetType(), "transitionToSecondStep", "transitionToSecondStep();", true);
        }

        protected void gvThursday_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvThursday.PageIndex = e.NewPageIndex;
            gvThursday.DataSource = thursdaylist;
            gvThursday.DataBind();

            pnlFirstStep.Visible = false;
            pnlSecondStep.Visible = true;
            Page.ClientScript.RegisterStartupScript(GetType(), "transitionToSecondStep", "transitionToSecondStep();", true);
        }

        protected void gvFriday_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvFriday.PageIndex = e.NewPageIndex;
            gvFriday.DataSource = fridaylist;
            gvFriday.DataBind();

            pnlFirstStep.Visible = false;
            pnlSecondStep.Visible = true;
            Page.ClientScript.RegisterStartupScript(GetType(), "transitionToSecondStep", "transitionToSecondStep();", true);
        }

        protected void gvSaturday_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvSaturday.PageIndex = e.NewPageIndex;
            gvSaturday.DataSource = saturdaylist;
            gvSaturday.DataBind();

            pnlFirstStep.Visible = false;
            pnlSecondStep.Visible = true;
            Page.ClientScript.RegisterStartupScript(GetType(), "transitionToSecondStep", "transitionToSecondStep();", true);
        }

        protected void gvSunday_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvSunday.PageIndex = e.NewPageIndex;
            gvSunday.DataSource = sundaylist;
            gvSunday.DataBind();

            pnlFirstStep.Visible = false;
            pnlSecondStep.Visible = true;
            Page.ClientScript.RegisterStartupScript(GetType(), "transitionToSecondStep", "transitionToSecondStep();", true);
        }

        protected void ExcelBut_Click(object sender, EventArgs e)
        {
            gvMonday.AllowPaging = false;
            gvMonday.DataSource = mondaylist;
            gvMonday.DataBind();

            gvTuesday.AllowPaging = false;
            gvTuesday.DataSource = tuesdaylist;
            gvTuesday.DataBind();

            gvWednesday.AllowPaging = false;
            gvWednesday.DataSource = wednesdaylist;
            gvWednesday.DataBind();

            gvThursday.AllowPaging = false;
            gvThursday.DataSource = thursdaylist;
            gvThursday.DataBind();

            gvFriday.AllowPaging = false;
            gvFriday.DataSource = fridaylist;
            gvFriday.DataBind();

            gvSaturday.AllowPaging = false;
            gvSaturday.DataSource = saturdaylist;
            gvSaturday.DataBind();

            gvSunday.AllowPaging = false;
            gvSunday.DataSource = sundaylist;
            gvSunday.DataBind();

            Response.Clear();
            Response.Buffer = true;
            Response.ClearContent();
            Response.ClearHeaders();
            Response.Charset = "";
            string FileName = "Housekeeping Report: " + DateTime.Now.ToShortDateString() + ".xls";
            StringWriter strwritter = new StringWriter();
            HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
            
            gvMonday.GridLines = GridLines.Both;
            gvMonday.HeaderStyle.Font.Bold = true;
            gvMonday.RenderControl(htmltextwrtter);
            Response.Write(strwritter.ToString());
            Response.Clear();
            
            gvTuesday.GridLines = GridLines.Both;
            gvTuesday.HeaderStyle.Font.Bold = true;
            gvTuesday.RenderControl(htmltextwrtter);
            Response.Write(strwritter.ToString());
            Response.Clear();

            gvWednesday.GridLines = GridLines.Both;
            gvWednesday.HeaderStyle.Font.Bold = true;
            gvWednesday.RenderControl(htmltextwrtter);
            Response.Write(strwritter.ToString());
            Response.Clear();

            gvThursday.GridLines = GridLines.Both;
            gvThursday.HeaderStyle.Font.Bold = true;
            gvThursday.RenderControl(htmltextwrtter);
            Response.Write(strwritter.ToString());
            Response.Clear();

            gvFriday.GridLines = GridLines.Both;
            gvFriday.HeaderStyle.Font.Bold = true;
            gvFriday.RenderControl(htmltextwrtter);
            Response.Write(strwritter.ToString());
            Response.Clear();

            gvSaturday.GridLines = GridLines.Both;
            gvSaturday.HeaderStyle.Font.Bold = true;
            gvSaturday.RenderControl(htmltextwrtter);
            Response.Write(strwritter.ToString());
            Response.Clear();

            gvSunday.GridLines = GridLines.Both;
            gvSunday.HeaderStyle.Font.Bold = true;
            gvSunday.RenderControl(htmltextwrtter);
            Response.Write(strwritter.ToString());

            Response.End();

            gvMonday.AllowPaging = true;
            gvMonday.DataSource = mondaylist;
            gvMonday.DataBind();

            gvTuesday.AllowPaging = true;
            gvTuesday.DataSource = tuesdaylist;
            gvTuesday.DataBind();

            gvWednesday.AllowPaging = true;
            gvWednesday.DataSource = wednesdaylist;
            gvWednesday.DataBind();

            gvThursday.AllowPaging = true;
            gvThursday.DataSource = thursdaylist;
            gvThursday.DataBind();

            gvFriday.AllowPaging = true;
            gvFriday.DataSource = fridaylist;
            gvFriday.DataBind();

            gvSaturday.AllowPaging = true;
            gvSaturday.DataSource = saturdaylist;
            gvSaturday.DataBind();

            gvSunday.AllowPaging = true;
            gvSunday.DataSource = sundaylist;
            gvSunday.DataBind();

        }



        protected void PDFBut_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/pdf";
            HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.pdf", "Housekeeping Report: " + DateTime.Now.ToShortDateString()));
            HttpContext.Current.Response.Charset = "utf-8";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            //For monday
            System.IO.StringWriter stringWrite1 = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite1 = new HtmlTextWriter(stringWrite1);
            DataGrid myDataGrid1 = new DataGrid();
            myDataGrid1.DataSource = mondaylist;
            myDataGrid1.DataBind();
            myDataGrid1.RenderControl(htmlWrite1);
            //For tuesday
            System.IO.StringWriter stringWrite2 = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite2 = new HtmlTextWriter(stringWrite2);
            DataGrid myDataGrid2 = new DataGrid();
            myDataGrid2.DataSource = tuesdaylist;
            myDataGrid2.DataBind();
            myDataGrid2.RenderControl(htmlWrite2);
            //Wednesday
            System.IO.StringWriter stringWrite3 = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite3 = new HtmlTextWriter(stringWrite3);
            DataGrid myDataGrid3 = new DataGrid();
            myDataGrid3.DataSource = wednesdaylist;
            myDataGrid3.DataBind();
            myDataGrid3.RenderControl(htmlWrite3);
            //Thursday
            System.IO.StringWriter stringWrite4 = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite4 = new HtmlTextWriter(stringWrite4);
            DataGrid myDataGrid4 = new DataGrid();
            myDataGrid4.DataSource = thursdaylist;
            myDataGrid4.DataBind();
            myDataGrid4.RenderControl(htmlWrite4);
            //Friday
            System.IO.StringWriter stringWrite5 = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite5 = new HtmlTextWriter(stringWrite5);
            DataGrid myDataGrid5 = new DataGrid();
            myDataGrid5.DataSource = fridaylist;
            myDataGrid5.DataBind();
            myDataGrid5.RenderControl(htmlWrite5);
            //Saturday
            System.IO.StringWriter stringWrite6 = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite6 = new HtmlTextWriter(stringWrite6);
            DataGrid myDataGrid6 = new DataGrid();
            myDataGrid6.DataSource = saturdaylist;
            myDataGrid6.DataBind();
            myDataGrid6.RenderControl(htmlWrite6);
            //Sunday
            System.IO.StringWriter stringWrite7 = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite7 = new HtmlTextWriter(stringWrite7);
            DataGrid myDataGrid7 = new DataGrid();
            myDataGrid7.DataSource = saturdaylist;
            myDataGrid7.DataBind();
            myDataGrid7.RenderControl(htmlWrite7);

            StringReader sr = new StringReader("Mondays:" + stringWrite1.ToString() + " " + "Tuesdays:" + stringWrite2.ToString() + " " + "Wednesdays:" + stringWrite3.ToString() + " " + "Thursdays:" + stringWrite4.ToString() + " " + "Fridays:" + stringWrite5.ToString() + " " + "Saturdays:" + stringWrite6.ToString() + " " + "Sundays:" + stringWrite7.ToString());
            Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
            pdfDoc.SetPageSize(PageSize.A4.Rotate());
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
            pdfDoc.Open();
            htmlparser.Parse(sr);
            pdfDoc.Close();
            HttpContext.Current.Response.Write(pdfDoc);
            HttpContext.Current.Response.End();


        }

        public override void VerifyRenderingInServerForm(Control control)
        {
            /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
               server control at run time. */
        }

        protected void PrintBut_Click(object sender, EventArgs e)
        {
            gvMonday.AllowPaging = false;
            gvMonday.DataSource = mondaylist;
            gvMonday.DataBind();

            gvTuesday.AllowPaging = false;
            gvTuesday.DataSource = tuesdaylist;
            gvTuesday.DataBind();

            gvWednesday.AllowPaging = false;
            gvWednesday.DataSource = wednesdaylist;
            gvWednesday.DataBind();

            gvThursday.AllowPaging = false;
            gvThursday.DataSource = thursdaylist;
            gvThursday.DataBind();

            gvFriday.AllowPaging = false;
            gvFriday.DataSource = fridaylist;
            gvFriday.DataBind();

            gvSaturday.AllowPaging = false;
            gvSaturday.DataSource = saturdaylist;
            gvSaturday.DataBind();

            gvSunday.AllowPaging = false;
            gvSunday.DataSource = sundaylist;
            gvSunday.DataBind();

            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            gvMonday.RenderControl(hw);

            StringWriter sw2 = new StringWriter();
            HtmlTextWriter hw2 = new HtmlTextWriter(sw2);
            gvTuesday.RenderControl(hw2);

            StringWriter sw3 = new StringWriter();
            HtmlTextWriter hw3 = new HtmlTextWriter(sw3);
            gvWednesday.RenderControl(hw3);

            StringWriter sw4 = new StringWriter();
            HtmlTextWriter hw4 = new HtmlTextWriter(sw4);
            gvThursday.RenderControl(hw4);

            StringWriter sw5 = new StringWriter();
            HtmlTextWriter hw5 = new HtmlTextWriter(sw5);
            gvFriday.RenderControl(hw5);

            StringWriter sw6 = new StringWriter();
            HtmlTextWriter hw6 = new HtmlTextWriter(sw6);
            gvSaturday.RenderControl(hw6);

            StringWriter sw7 = new StringWriter();
            HtmlTextWriter hw7 = new HtmlTextWriter(sw7);
            gvSunday.RenderControl(hw7);

            string gridHTML = sw.ToString().Replace("\"", "'")
                .Replace(System.Environment.NewLine, "")
                ;

            string gridHTML2 = sw2.ToString().Replace("\"", "'")
               .Replace(System.Environment.NewLine, "")
               ;

            string gridHTML3 = sw3.ToString().Replace("\"", "'")
               .Replace(System.Environment.NewLine, "")
               ;

            string gridHTML4 = sw4.ToString().Replace("\"", "'")
               .Replace(System.Environment.NewLine, "")
               ;

            string gridHTML5 = sw5.ToString().Replace("\"", "'")
               .Replace(System.Environment.NewLine, "")
               ;

            string gridHTML6 = sw6.ToString().Replace("\"", "'")
               .Replace(System.Environment.NewLine, "")
               ;

            string gridHTML7 = sw7.ToString().Replace("\"", "'")
               .Replace(System.Environment.NewLine, "")
               ;

            StringBuilder sb = new StringBuilder();

            sb.Append("<script type = 'text/javascript'>");
            sb.Append("window.onload = new function(){");
            sb.Append("var printWin = window.open('', '', 'left=0");
            sb.Append(",top=0,width=1000,height=600,status=0');");
            sb.Append("printWin.document.write(\"");
            sb.Append(gridHTML);


            sb.Append(gridHTML2);


            sb.Append(gridHTML3);


            sb.Append(gridHTML4);


            sb.Append(gridHTML5);


            sb.Append(gridHTML6);


            sb.Append(gridHTML7);
            sb.Append("\");");
            sb.Append("printWin.document.close();");
            sb.Append("printWin.focus();");
            sb.Append("printWin.print();");

            sb.Append("printWin.close();};");
            sb.Append("</script>");
            ClientScript.RegisterStartupScript(this.GetType(), "Room Status Report", sb.ToString());

            gvMonday.AllowPaging = true;
            gvMonday.DataSource = mondaylist;
            gvMonday.DataBind();

            gvTuesday.AllowPaging = true;
            gvTuesday.DataSource = tuesdaylist;
            gvTuesday.DataBind();

            gvWednesday.AllowPaging = true;
            gvWednesday.DataSource = wednesdaylist;
            gvWednesday.DataBind();

            gvThursday.AllowPaging = true;
            gvThursday.DataSource = thursdaylist;
            gvThursday.DataBind();

            gvFriday.AllowPaging = true;
            gvFriday.DataSource = fridaylist;
            gvFriday.DataBind();

            gvSaturday.AllowPaging = true;
            gvSaturday.DataSource = saturdaylist;
            gvSaturday.DataBind();

            gvSunday.AllowPaging = true;
            gvSunday.DataSource = sundaylist;
            gvSunday.DataBind();
        }  
    }
}